package com.xu.cloud.common.desensitization;

import com.xu.cloud.common.constant.Constant;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Objects;

/**
 * 正则表达式脱敏规则
 *
 * @author xuguan
 * @since 2025/3/3
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RegexDesensitizationRule implements IDesensitizationRule {

    public static final RegexDesensitizationRule DEFAULT_RULE
            = new RegexDesensitizationRule("^.*$", "******");

    /**
     * 正则表达式
     */
    private String regex;

    /**
     * 替换表达式
     */
    private String replacement;

    @Override
    public String desensitization(String source) {
        if (Objects.isNull(source)) {
            return Constant.EMPTY_STRING;
        }
        return source.replaceAll(regex, replacement);
    }
}
